﻿using Max.PH4_2104A.Shopping.ThreeFrame.BLL;
using Max.PH4_2104A.Shopping.ThreeFrame.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Max.PH4_2104A.Shopping.ThreeFrame.API.Controllers
{

    /// <summary>
    /// 系统用户的控制器
    /// </summary>
    [Route("system/user")]
    [ApiController]
    public class SystemUserController : ControllerBase
    {
        SystemUserBLL _bll;

        public SystemUserController(SystemUserBLL bll)
        {
            _bll = bll;
        }

        /// <summary>
        /// 添加系统用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Insert(SystemUserModel model)
        {
            //调用业务层方法
            var result = _bll.Insert(model);

            //统一返回值（暂时不用dto，用的是匿名类）
            return Ok(new { state = result.Item1, message = result.Item2 });
        }

        /// <summary>
        /// 系统用户登录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("login")]
        public IActionResult Login(SystemUserModel model)
        {
            //调用业务层
            var result = _bll.Login(model.LoginName, model.Password);

            return Ok(new { state = result.Item1, message = result.Item2, data = result.Item3 });
        }


        /// <summary>
        /// 获取系统用户
        /// </summary>
        /// <param name="name">用户姓名</param>
        /// <param name="pageIndex">页码（第几页）</param>
        /// <param name="pageSize">页大小（每页显示几条）</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Get(string? name = "", int pageIndex = 1, int pageSize = 10)
        {
            //判断参数
            pageIndex = pageIndex <= 0 ? 1 : pageIndex;
            pageSize = pageSize <= 0 ? 10 : pageSize;

            //调用业务
            var d = _bll.Query(name, pageIndex, pageSize, out int total);

            //统一返回值（暂时不用dto，用的是匿名类）
            return Ok(new { state = true, message = "操作成功", data = d, total });
        }

        /// <summary>
        /// 批量修改状态
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult Put(List<SystemUserModel> models)
        {

            _bll.Update(models);

            return Ok(new { state=true,message="修改成功"});
        }


        //扩展方法




        

    }


    
}
